package cn.com.generaldata.os.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.List;

import com.google.common.collect.Lists;

//import org.apache.catalina.util.MD5Encoder;

public class FileUtil {

	public static void main(String[] args) throws Exception {
		List<String> result=Lists.newArrayList();
		List<String> groupRel = Lists.newArrayList("asdf","jhhd","asdfg");
		List<String> dustryReportList = Lists.newArrayList("asdf","asdfg","aaaa");
		//集合取交集
		groupRel.retainAll(dustryReportList);
		for(String sn:groupRel){
			System.out.println(sn);
		}
//		Double x=612314238d;
//		 DecimalFormat df = new DecimalFormat("###0");
//		 System.out.println(df.format(x));  
//		int i = 7;
//		System.out.println(i % 3 == 0 ? i / 3 : (i / 3) + 1);
		//		//累计值
		//		BigDecimal dataValueSum = new BigDecimal(19.853);
		//		//上年累计值
		//		BigDecimal dataValueSumLast = new BigDecimal(11.223);
		//		System.out.println((dataValueSum.subtract(dataValueSumLast)).divide(dataValueSumLast, 2));
		//		Double value = (69.2 - 100.56) / 100.56;
		//		//		int indexOf = percent.indexOf("%");
		//		//		String substring = percent.substring(0, indexOf);
		//		if (value >= 0.3 || value <= -0.3)
		//			System.out.println(value);
		//		Context context = Lang.context();
		//		El exp = new El("10+(4-3)==(5+5)+2");
		//		System.out.println(exp.eval(context));
		//		String formualStr = "i_25796 &gt;= i_25797+i_25798+i_25799";
		//		String[] split = formualStr.split("+|-|*|/|>=|>|<=|<|==");
		//		for (String str : split)
		//			System.out.println(str);
		// 输出为  false
//				String service = "11002";
//				String username = "admin";
//				String timeStr="2017-09-01 17-25-00";
//				String userPasswd = "21940ff45546b3e9a1f3931761083903";
//				String enPW = service + username + timeStr + userPasswd;
//				String encode = new DefaultPasswordEncoder("MD5").encode(enPW);
//				System.out.println("加密后密码：" + encode);
		//		//		String unitAreaId = \"32\";
		//		//		NumberFormat pf = NumberFormat.getInstance();
		//		//		pf.setMaximumFractionDigits(2); // 小数点最大位数
		//		//		pf.setMinimumFractionDigits(0);
		//		//		double num = Double.valueOf(4206);
		//		//		Double result = 0d;
		//		//		if (num > 0) {
		//		//			result = Double.valueOf(3) / num * 100;
		//		//		}
		//		//		//		String addZeroForNum = CommonUtil.addZeroForNum(unitAreaId, 15);
		//		//		System.out.println(pf.format(result));
	}
	
	public static boolean createDir(String destDirName) {  
        File dir = new File(destDirName);  
        if (dir.exists()) {  
            System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");  
            return false;  
        }  
        if (!destDirName.endsWith(File.separator)) {  
            destDirName = destDirName + File.separator;  
        }  
        //创建目录  
        if (dir.mkdirs()) {  
            System.out.println("创建目录" + destDirName + "成功！");  
            return true;  
        } else {  
            System.out.println("创建目录" + destDirName + "失败！");  
            return false;  
        }  
    }  
	
	 public static boolean createFile(String destFileName) {  
	        File file = new File(destFileName);  
	        if(file.exists()) {  
	            System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");  
	            return false;  
	        }  
	        if (destFileName.endsWith(File.separator)) {  
	            System.out.println("创建单个文件" + destFileName + "失败，目标文件不能为目录！");  
	            return false;  
	        }  
	        //判断目标文件所在的目录是否存在  
	        if(!file.getParentFile().exists()) {  
	            //如果目标文件所在的目录不存在，则创建父目录  
	            System.out.println("目标文件所在目录不存在，准备创建它！");  
	            if(!file.getParentFile().mkdirs()) {  
	                System.out.println("创建目标文件所在目录失败！");  
	                return false;  
	            }  
	        }  
	        //创建目标文件  
	        try {  
	            if (file.createNewFile()) {  
	                System.out.println("创建单个文件" + destFileName + "成功！");  
	                return true;  
	            } else {  
	                System.out.println("创建单个文件" + destFileName + "失败！");  
	                return false;  
	            }  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	            System.out.println("创建单个文件" + destFileName + "失败！" + e.getMessage());  
	            return false;  
	        }  
	    }
	 
	 public static boolean writeTxtFile(String content,File  fileName)throws Exception{  
		  RandomAccessFile mm=null;  
		  boolean flag=false;  
		  FileOutputStream o=null;  
		  try {  
		   o = new FileOutputStream(fileName);  
		      o.write(content.getBytes("UTF-8"));  
		      o.close();  
		   flag=true;  
		  } catch (Exception e) {  
		   // TODO: handle exception  
		   e.printStackTrace();  
		  }finally{  
		   if(mm!=null){  
		    mm.close();  
		   }  
		  }  
		  return flag;  
		 }  
	 
	 /** 
	  * 读TXT文件内容 
	  * @param fileName 
	  * @return 
	  */  
	 public static String readTxtFile(File fileName)throws Exception{  
	  String result=null;  
	  FileReader fileReader=null;  
	  BufferedReader bufferedReader=null;  
	  try{
	InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
	   //fileReader=new FileReader(fileName);  
	   bufferedReader=new BufferedReader(isr);  
	   try{  
	    String read=null;  
	    while((read=bufferedReader.readLine())!=null){  
	     result=result+read+"\r\n";  
	    }  
	   }catch(Exception e){  
	    e.printStackTrace();  
	   }  
	  }catch(Exception e){  
	   e.printStackTrace();  
	  }finally{  
	   if(bufferedReader!=null){  
	    bufferedReader.close();  
	   }  
	   if(fileReader!=null){  
	    fileReader.close();  
	   }  
	  }  
	  System.out.println("读取出来的文件内容是："+"\r\n"+result);  
	  return result;  
	 } 
}
